package com.itheima.controller;


import com.itheima.commo.R;
import com.itheima.entity.User;
import com.itheima.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.Map;

/**
 * 用户控制层类
 */
@Slf4j
@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;

    /**
     * 手机号码发送验证码
     */
    @GetMapping("/sendMsg")
    public R<Integer> sendMsg(String phone, HttpSession httpSession) throws Exception {
        Integer code = userService.sendMsg(phone, httpSession);
        return R.success(code, "发送验证码成功");
    }


    /**
     * 登录校验
     * 移动端验证码登录
     * 方式二：Map接收  springmvc页面数据进行转换map
     */
    @PostMapping("/login")
    public R<User> login(@RequestBody Map<String,String> params,HttpSession httpSession){
        String phone = params.get("phone");
        String code = params.get("code");
        userService.login(phone, code, httpSession);
        return R.success("登录成功了");
    }

    /**
     * 用户端退出
     */
    @PostMapping("/loginout")
    public R<String> logInout(HttpSession httpSession){
        try {
            userService.loginout(httpSession);
            return R.success("退出成功了");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return R.success("退出失败了");
    }








}
